package com.zhou.common.mybatis.template.shiro.nacos.listener;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.listener.Listener;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;

/**
 * @author 18576666986
 * @description
 * @createDate 2022/10/24 11:42
 */

@Slf4j
@Configuration
@RefreshScope
@EnableConfigurationProperties(SpecialRoleProperties.class)
public class SpecialRoleConfig implements InitializingBean {

    @Autowired
    private SpecialRoleProperties specialRoleProperties;

    @Value("${spring.application.name}")
    private String appName;
    @Autowired
    private NacosConfigManager nacosConfigManager;

    @Autowired
    private NacosConfigProperties configProperties;

    /**
     * s1 角色对应的数字未 2的0次方
     * s2 角色对应的数字未 2的1次方
     * s3 角色对应的数字未 2的2次方
     * s4 角色对应的数字未 2的3次方
     */
    public static Pattern s1 = Pattern.compile("spzx:admin");
    public static Pattern s2 = Pattern.compile("spzx:manage");
    public static Pattern s3 = Pattern.compile("pay_center:admin");
    public static Pattern s4 = Pattern.compile("pay_center:bus_manager");
    public static Pattern s5 = Pattern.compile("省领导");
    public static Pattern s6 = Pattern.compile("省领导");
    public static Pattern s7 = Pattern.compile("省领导");
    public static Pattern s8 = Pattern.compile("省领导");
    public static Pattern s9 = Pattern.compile("省领导");
    public static Pattern s10 = Pattern.compile("省领导");


    public void onMessage(String msg) {
        log.info("nacos配置文件变化，已监听到。。。");
        List<String> roles = specialRoleProperties.getRoles();
        if (roles.size() == 0) {
            return;
        }
        for (int i = 0; i < roles.size(); i++) {
            switch (i) {
                case 0:
                    s1 = Pattern.compile(roles.get(i));
                    break;
                case 1:
                    s2 = Pattern.compile(roles.get(i));
                    break;
                case 2:
                    s3 = Pattern.compile(roles.get(i));
                    break;
                case 3:
                    s4 = Pattern.compile(roles.get(i));
                    break;
                case 4:
                    s5 = Pattern.compile(roles.get(i));
                    break;
                case 5:
                    s6 = Pattern.compile(roles.get(i));
                    break;
                case 6:
                    s7 = Pattern.compile(roles.get(i));
                    break;
                case 7:
                    s8 = Pattern.compile(roles.get(i));
                    break;
                case 8:
                    s9 = Pattern.compile(roles.get(i));
                    break;
                case 9:
                    s10 = Pattern.compile(roles.get(i));
                    break;
                default:
            }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        onMessage(null);
        nacosConfigManager.getConfigService().addListener(appName + ".yaml", configProperties.getGroup(),
                new Listener() {
                    @Override
                    public Executor getExecutor() {
                        return Executors.newSingleThreadExecutor();
                    }

                    @SneakyThrows
                    @Override
                    public void receiveConfigInfo(String configInfo) {
                        Thread.sleep(5000);
                        onMessage(configInfo);
                    }
                });
    }
}
